using DataFrames
using QuadGK
using Distributions
using JLD
using Optim
using ForwardDiff
using CSV
using LinearAlgebra
using SpecialFunctions
using Random
using DelimitedFiles
using SparseArrays

clearconsole()

#-------------------------------------------------------------
# include Functions
#-------------------------------------------------------------
#cd("$(pwd())/Dropbox/Heterogeneity/Software/KS_Simulation/")
readDir = "$(pwd())/Functions/"
include(readDir *"vech.jl");
#include(readDir *"VAR_Procedures.jl");
include(readDir *"VAR_MoreLags_Procedures.jl");
include(readDir *"Loaddata.jl");

#-------------------------------------------------------------
# choose specification files
#-------------------------------------------------------------
nDataSel    = "3"
nVARMDDSpec = "3P"
specDir     = "$(pwd())/SpecFiles/"
include(specDir * "/AltVARMDDspec" * nVARMDDSpec * ".jl")

#-------------------------------------------------------------
# load aggregate and alt data
#-------------------------------------------------------------
dataDir  = "$(pwd())/data/Para" * nDataSel *"/"
agg_data = loadaggdata(1,Tend)
n_agg    = size(agg_data)[2]
percentiles_data, vec_percs, gini_coef_data, probMass_above15_data, probMass_above20_data = loadaltdata(1,Tend)

#-------------------------------------------------------------
# Combine agg and alt data
#-------------------------------------------------------------
if nSpecStr == "Pctl"
    data    = [ agg_data percentiles_data ]
    n_cross = size(percentiles_data)[2]
else
    data    = [ agg_data gini_coef_data probMass_above20_data]
    n_cross = 2
end
n = n_agg + n_cross

#-------------------------------------------------------------
# Compute MDD over hyperparameter grid
#-------------------------------------------------------------

MDD_vec = zeros(hyper_n)

for ll = 1:hyper_n

    # Extract hyperparaters
    lambda1 = hyper_grid[ll,1]
    lambda2 = hyper_grid[ll,2]
    lambda3 = hyper_grid[ll,3]
    lambda4 = 2
    lambda  = [lambda1 lambda2 lambda3 lambda4]
    nlags   = Int(hyper_grid[ll,4])

    # Adjust the sample
    data_adj = data[Tdrop-nlags+1:end,:]

    # OLS estimation
    YY, XX, PHIols, Sols, SIGMAols = data_to_YY(data_adj, nlags, nlags)

    # Generate Prior
    igammadf    = size(YY)[2]+sigdf
    prior       = prior_ACP_stru(YY, SIGMAols, n_agg, igammadf, nlags, lambda)

    # Compute MDD
    MDD_vec[ll] = ml_VAR_ACP(nlags,YY,XX,prior)

end


#-------------------------------------------------------------
# Save output
#-------------------------------------------------------------

sName    = "AltVAR_MDD" * nVARMDDSpec * "_" * nSpecStr
savedir  = "$(pwd())/results/Para" *nDataSel * "/" * sName *"/";
try mkdir(savedir) catch; end

lambda_MDD = [hyper_grid MDD_vec]
sNameCols  = ["Lam1", "Lam2", "Lam3", "Nlags", "MDD"]
sNameFile  = "AltVAR_MDD" * nVARMDDSpec * "_" * nSpecStr
CSV.write(savedir * sNameFile * "_lambda_MDD.csv", DataFrame(permutedims(sNameCols),:auto), header=false)
CSV.write(savedir * sNameFile * "_lambda_MDD.csv", DataFrame(lambda_MDD,:auto), append=true);
